package cn.adviceclient.features.module;

import cn.adviceclient.AdviceClient;
import cn.adviceclient.features.module.Module;
import cn.adviceclient.features.module.impl.movement.Sprint;
import cn.adviceclient.features.module.impl.render.*;
import cn.adviceclient.features.value.AbstractSetting;

import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public final class ModuleManager {
    public static final CopyOnWriteArrayList<Module> modules = new CopyOnWriteArrayList<>();
    private ModuleManager() {}

    public static void initialize() {
        AdviceClient.LOGGER.info("ModuleManager is initializing!");

        addModule(new HUD());
        addModule(new ClickGui());
        addModule(new Sprint());
        addModule(new ESP2D());
    }

    private static void addModule(Module module) {
        for (Field field : module.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try {
                Object obj = field.get(module);
                if (obj instanceof AbstractSetting<?>) {
                    module.settings.add((AbstractSetting<?>) obj);
                }
            } catch (IllegalAccessException ignored) {}
        }
        modules.add(module);
    }

    public static <T extends Module> T getModule(Class<T> clazz) {
        for (Module module : modules) {
            if (clazz.isInstance(module)) {
                return clazz.cast(module);
            }
        }
        return null;
    }

    public static CopyOnWriteArrayList<Module> getModules(){
        return modules;
    }

}